package com.abigdreamer.im.client.module.main.weather;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Map;

import com.abigdreamer.im.client.module.main.EIMMainFrame;
import com.abigdreamer.im.client.util.EIMLogger;
import com.abigdreamer.swing.animation.Invoker;
import com.abigdreamer.swing.app.SwingUtil;
import com.abigdreamer.swing.component.JCLabel;

public class WeatherLabel extends JCLabel {

	private static final long serialVersionUID = -1522713440797597011L;

	protected String weatherImg = "sunny";
	protected Map<String, Map<String, String>> weatherReport;
	private WeatherPanel weatherPanel;

	public WeatherLabel() {

		EIMLogger.INFO("开始获取天气信息...");

		getWeather();

		EIMLogger.INFO("获取天气信息结束...");

		setIcon(SwingUtil.getIcon("weather/" + weatherImg + ".png", 56, 56));

		EIMLogger.INFO("添加天气面板事件...");
		addMouseListener(new MouseAdapter() {

			public void mouseEntered(MouseEvent e) {

				setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
				weatherPanel = new WeatherPanel(WeatherLabel.this);
				EIMMainFrame v = EIMMainFrame.getInstance();

				weatherPanel.setLocation(v.getX() - weatherPanel.getWidth(), v.getY());
				// weatherPanel.setVisible(true);
				Invoker.show(weatherPanel);
			}

			public void mouseExited(MouseEvent e) {
				setBackground(Color.white);
				setCursor(Cursor.getDefaultCursor());
				weatherPanel.setVisible(false);
			}

		});

		EIMLogger.INFO("添加天气面板事件结束...");
	}

	public static String getWeatherImage(String icon) {
		System.out.println(icon);
		if (icon.contains("cn_cloudy")) {
			return "cloudy";
		} else if (icon.contains("cn_heavyrain")) {
			return "heavyrain";
		} else if (icon.contains("mostly_sunny")) {
			return "mostly_sunny";
		} else if (icon.contains("sunny")) {
			return "sunny";
		} else if (icon.contains("haze")) {
			return "haze";
		}

		return "sunny";
	}

	private void getWeather() {

		weatherReport = GoogleWeather.getWeather("nanjing");

		// java.awt.EventQueue.invokeLater(new Runnable() {
		// public void run() {
		// EIMLoginWindow.getInstance().setVisible(true);
		// }
		// });

		if (null != weatherReport) {

			Map<String, String> keyValues = weatherReport.get("current_conditions");
			weatherImg = getWeatherImage(keyValues.get("icon"));
			System.out.println("******************当前天气***********************");

			keyValues = weatherReport.get("forecast_conditions1");

			System.out.println("******************第一天***********************");

			keyValues = weatherReport.get("forecast_conditions2");

			System.out.println("******************第二天***********************");

			keyValues = weatherReport.get("forecast_conditions3");

			System.out.println("******************第三天***********************");

		} else {
			System.out.print("获取天气失败或不存在此城市");
		}
	}

}
